﻿using System.Collections.Generic;
using StockholmsStad.Data.Entity;
using StockholmsStad.Runtime.Serialization.DataContract;
using Address=StockholmsStad.Data.Entity.Address;
using Minister=StockholmsStad.Data.Entity.Minister;
using Person=StockholmsStad.Data.Entity.Person;
using System;
using StockholmsStad.Runtime.Serialization.DataContract.Enums;

namespace StockholmsStad.Data.ContractFactory
{
    /// <summary>
    /// Konvertering befintligt dataobjekt till kontraktsobjekt
    /// </summary>
    internal class MinisterFactory
    {
        public Runtime.Serialization.DataContract.Minister CreateContractMinister(FindMinistersResult result)
        {
            var minister = new Runtime.Serialization.DataContract.Minister
                               {
                                   FirstNames = result.FirstNames,
                                   LastName = result.LastName,
                                   Address = new Runtime.Serialization.DataContract.Address
                                                 {
                                                     City = result.City,
                                                     CoAddress = result.CareOf,
                                                     Country = result.Country,
                                                     PostCode = result.PostCode,
                                                     StreetAddress = result.StreetAddress
                                                 },

                                   BirthdayNumber = result.PersonalNumber,
                                   CellPhoneNumber = result.CellPhoneNumber,
                                   Email = result.EmailAddress,
                                   MinisterCode = result.MinisterCode,
                                   MinisterId = result.MinisterId,
                                   Note = result.Note,
                                   PhoneNumber = result.PhoneNumber,
                                   SpecialAssignment = result.SpecialAssignment.Value,
                               };

            if (result.AssignmentStartDate.HasValue)
                minister.AssignmentStartDate = result.AssignmentStartDate.Value;

            if (result.AssignmentEndDate.HasValue)
                minister.AssignmentEndDate = result.AssignmentEndDate.Value;

            return minister;
        }

        public Minister CreateEntityObject(Runtime.Serialization.DataContract.Minister minister)
        {
            var ministerEntity = new Minister() {Person = new Person() {Address = new Address()}};
            UpdateEntityObject(minister, ministerEntity);
            return ministerEntity;
        }

        public void UpdateEntityObject(Runtime.Serialization.DataContract.Minister minister, Minister ministerEntity)
        {
            ministerEntity.Person.PersonalNumber = minister.BirthdayNumber;
            ministerEntity.MinisterCode = minister.MinisterCode;
            ministerEntity.Note = minister.Note;
            ministerEntity.AssignmentStartDate = minister.AssignmentStartDate;
            ministerEntity.AssignmentEndDate = minister.AssignmentEndDate;
            ministerEntity.SpecialAssignment = minister.SpecialAssignment;
            PersonFactory personFactory = FactoryAgent.GetPersonFactory();
            personFactory.UpdateEntityObject(minister, ministerEntity.Person);
        }

        public Runtime.Serialization.DataContract.Minister CreateContractObject(Minister ministerEntity)
        {
            AddressFactory addressFactory = FactoryAgent.GetAddressFactory();
            var contractObject = new Runtime.Serialization.DataContract.Minister
                                     {
                                         MinisterCode = ministerEntity.MinisterCode,
                                         FirstNames = ministerEntity.Person.FirstNames,
                                         LastName = ministerEntity.Person.LastName,
                                         BirthdayNumber = ministerEntity.Person.PersonalNumber,
                                         Address = addressFactory.CreateContractObject(ministerEntity.Person.Address),
                                         AssignmentStartDate = ministerEntity.AssignmentStartDate,
                                         AssignmentEndDate = ministerEntity.AssignmentEndDate,
                                         CellPhoneNumber = ministerEntity.Person.CellPhoneNumber,
                                         Email = ministerEntity.Person.EmailAddress,
                                         Note = ministerEntity.Note,
                                         PhoneNumber = ministerEntity.Person.PhoneNumber,
                                         MinisterId = ministerEntity.MinisterId,
                                         SpecialAssignment = ministerEntity.SpecialAssignment,
                                     };

            return contractObject;
        }

        public void CreateContractLanguages(Runtime.Serialization.DataContract.Minister minister,
                                            List<ValueIdPair> languages)
        {
            foreach (ValueIdPair lang in languages)
            {
                if (minister.Languages == null)
                    minister.Languages = new List<string>();

                minister.Languages.Add(lang.Name);
            }
        }

        public MinisterLanguage CreateEntityObject(int languageId, int ministerId)
        {
            return new MinisterLanguage() {LanguageId = languageId, MinisterId = ministerId};
        }

        public MinisterTimeBlock CreateContractTimeBlock(GetMinisterTimeBlocksResult timeBlocksResult)
        {
            DateTime startdate;
            var timeBlock = new MinisterTimeBlock();

            if(DateTime.TryParse(timeBlocksResult.StartDate, out startdate))
                timeBlock.StartDateTime = startdate;

            timeBlock.StartTime = timeBlocksResult.StartTime.Value;
            timeBlock.EndTime = timeBlocksResult.EndTime.Value;

            // Vi är bara intresserade att veta ifall förrättaren är ord. eller inte. Null-check duger
            if (timeBlocksResult.MinisterId.HasValue)
                timeBlock.Minister = new Runtime.Serialization.DataContract.Minister
                                         {MinisterId = timeBlocksResult.MinisterId.Value};

            if (timeBlocksResult.TempMinisterId.HasValue)
                timeBlock.TempMinister = new Runtime.Serialization.DataContract.Minister { MinisterId = timeBlocksResult.TempMinisterId.Value };

            // Status är inte aktuellt när man bara visar tider
            timeBlock.TimeBlockStatus = TimeBlockStatus.Unspecified;
            return timeBlock;
        }

        public Runtime.Serialization.DataContract.Minister CreateContractObject(GetMinisterByTicketResult ministerForBookingResult)
        {
            return new Runtime.Serialization.DataContract.Minister
                       {
                           MinisterId = ministerForBookingResult.MinisterId,
                           FirstNames = ministerForBookingResult.MinisterFirstName,
                           LastName = ministerForBookingResult.MinisterLastName,
                       };
        }
    }
}